﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OilRigSumoWrestlers.Controllables.Controllers;
using OilRigSumoWrestlers.Entities.MenuItems;
using OilRigSumoWrestlers.Entities.MenuItems.NetworkItems;
using OilRigSumoWrestlers.Scenes.Menu;
using RaptorEngine;
using RaptorEngine.Components;
using RaptorEngine.Components.Drawable;
using RaptorEngine.Managers;

namespace OilRigSumoWrestlers.Scenes.Network
{
    class Lobby : MenuScene
    {
        private Entity _gamer1Name, _gamer1Status, _gamer2Name, _gamer2Status;

        private Vector2 worldCenter = new Vector2(
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.X +
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Width / 2,
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Y +
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
        
        public Lobby(bool network) : base(network)
        {
            CreateHUD();
            CreateController();
        }

        private void CreateHUD()
        {
            SpriteFont statusFont = GameEngine.Instance.Load<SpriteFont>(@"Fonts/LobbyFont");
            SpriteFont nameFont = GameEngine.Instance.Load<SpriteFont>(@"Fonts/LobbyNameFont");

            // Player 1
            _gamer1Name = new Entity(worldCenter - new Vector2(425, 120));
            _gamer1Name.AddComponent(new DrawableText(_gamer1Name, nameFont, "", Color.White, 1, true));

            _gamer1Status = new Entity(worldCenter - new Vector2(425, 80));
            _gamer1Status.AddComponent(new DrawableText(_gamer1Status, statusFont, "", Color.White, 1, true));

            // Player 2
            _gamer2Name = new Entity(worldCenter + new Vector2(375, -120));
            _gamer2Name.AddComponent(new DrawableText(_gamer2Name, nameFont, "", Color.White, 1, true));

            _gamer2Status = new Entity(worldCenter + new Vector2(375, -80));
            _gamer2Status.AddComponent(new DrawableText(_gamer2Status, statusFont, "", Color.White, 1, true));

            AddEntity(_gamer1Name);
            AddEntity(_gamer1Status);
            AddEntity(_gamer2Name);
            AddEntity(_gamer2Status);

            // Return menu
            DrawableText optReadySelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 80)),
                                                  GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"),
                                                  "Ready", Color.White, 1, true);
            DrawableText optReady = new DrawableText(new Entity(worldCenter + new Vector2(0, 80)),
                                           GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), "Ready",
                                           Color.Gray, 1, true);

            DrawableText optBackSelected = new DrawableText(new Entity(worldCenter + new Vector2(0, 120)),
                                                   GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"),
                                                   "Back", Color.White, 1, true);
            DrawableText optBack = new DrawableText(new Entity(worldCenter + new Vector2(0, 120)),
                                           GameEngine.Instance.Load<SpriteFont>(@"Fonts/MenuOptions"), "Back",
                                           Color.Gray, 1, true);

            _menuItems.Add(new MultiplayerLobbyItem(optReadySelected, optReady));
            _menuItems.Add(new MultiplayerSetupItem(optBackSelected, optBack));
            _menuItems[_selectedItem].Select();

            foreach (MenuItem item in _menuItems)
            {
                AddEntity(item);
            }
            
        }

        private void CreateController()
        {
            Entity menuController = new Entity();
            menuController.AddComponent(new MenuControllable(menuController, this, LogicalPlayerIndex.One));
            AddEntity(menuController);
        }

        public override void GoBack()
        {
            GameEngine.Instance.Network.State = NetworkManager.NetworkState.Ending;
            GameEngine.Instance.Scene.HideCurrentScene();
            GameEngine.Instance.Scene.StopCurrentScene();
            GameEngine.Instance.Scene.RemoveCurrentScene();
            GameEngine.Instance.Scene.PushScene(new SetupGame(false));
            GameEngine.Instance.Scene.StartCurrentScene();
            GameEngine.Instance.Scene.ShowCurrentScene();
        }

        public override void Update()
        {
            // Check the amount of players connected, and display updated info.
            if (GameEngine.Instance.Network.Gamers.Count == 0)
            {
                GameEngine.Instance.Scene.RemoveCurrentScene();
                GameEngine.Instance.Scene.StartCurrentScene();
                GameEngine.Instance.Scene.ShowCurrentScene();
            }
            if (GameEngine.Instance.Network.Gamers.Count == 1)
            {
                ((Drawable)_gamer1Name.GetComponent(ComponentType.Drawable2D)).TextString = GameEngine.Instance.Network.Gamers[0].Gamertag;
                ((Drawable)_gamer2Name.GetComponent(ComponentType.Drawable2D)).TextString = LobbyConstants.MATCHMAKING;
            }
            if (GameEngine.Instance.Network.Gamers.Count == 2)
            {
                ((Drawable)_gamer1Name.GetComponent(ComponentType.Drawable2D)).TextString = GameEngine.Instance.Network.Gamers[0].Gamertag;
                ((Drawable)_gamer2Name.GetComponent(ComponentType.Drawable2D)).TextString = GameEngine.Instance.Network.Gamers[1].Gamertag;

                // Check if ready.
                ((Drawable)_gamer1Status.GetComponent(ComponentType.Drawable2D)).TextString = GameEngine.Instance.Network.CheckPlayerStatus(0) ? LobbyConstants.READY : LobbyConstants.NOT_READY;
                ((Drawable)_gamer2Status.GetComponent(ComponentType.Drawable2D)).TextString = GameEngine.Instance.Network.CheckPlayerStatus(1) ? LobbyConstants.READY : LobbyConstants.NOT_READY;

                // If everyone is ready start the game.
                if (GameEngine.Instance.Network.CheckPlayerStatus(0) && GameEngine.Instance.Network.CheckPlayerStatus(1))
                {
                    GameEngine.Instance.Scene.HideCurrentScene();
                    GameEngine.Instance.Scene.StopCurrentScene();
                    GameEngine.Instance.Scene.RemoveCurrentScene();
                    GameEngine.Instance.Scene.PushScene(new NetworkGame(true));
                    GameEngine.Instance.Scene.StartCurrentScene();
                    GameEngine.Instance.Scene.ShowCurrentScene();
                    // Change state again.
                    GameEngine.Instance.Network.SwitchState();
                }
            }
            if (GameEngine.Instance.Network.State == NetworkManager.NetworkState.Ending)
            {
                GoBack();
            }
        }
    }

    class LobbyConstants
    {
        public const string NOT_READY = "Not ready";
        public const string READY = "Ready";
        public const string MATCHMAKING = "Matchmaking..";
    }
}
